<p>
  This low-frequency momentum trading strategies are applied to daily data on seven kinds of exchange rates. We use five years history data before January 2011 for initial estimation of the trend model. Daily exchange rates for the period January 2011 to May 2017 is used for out of sample trading.
</p>

<div class="section-example-container">

<pre class="python">def Initialize(self):
    self.SetStartDate(2011,1,1)
    self.SetEndDate(2017,5,30)
    self.SetCash(100000)
    self.numdays = 360*5  # set the length of training period
    self.syl = self.AddSecurity(SecurityType.Forex, "EURUSD", Resolution.Daily).Symbol
    self.n,self.m = 2, 1
    self.trend = None
    self.SetBenchmark(self.syl)
    self.MA_rules = None
    history = self.History(self.numdays,Resolution.Daily)
    self.close = [slice[self.syl].Close for slice in history]
</pre>
</div>

<h3>Step 1: Calibrating the Filter Smoothing Parameter λ</h3>
<p>
  The Ravn–Uhlig rule is commonly used to set the smoothing parameter λ in HP filter and must be greater than 0. It is adjusted by the changing frequency of observations and must be greater than 0. Hodrick and Prescott (1997) recommended setting λ to 1,600 for quarterly data. The Ravn–Uhlig rule sets  \(\lambda = 1600p^4\) , where p is the number of periods per quarter. As for our daily exchange rate data, we should have set λ to be \(1600\times (30 \times 4)^4\). But when we use this value as the λ, the curve is almost a straight line since the trend becomes smoother as λ → ∞. In order to avoid excessive smoothing, we gradually decrease the λ and draw the smoothing curve. Below is the chart of EURUSD daily price from the year 2010 to 2011. t100 denotes the trend component after filter with the parameter λ=100.
</p>

<img class="img-responsive" src="https://cdn.quantconnect.com/tutorials/i/Tutorial08-low-frequency-component-more.jpg" alt="low-frequency-component-more" />

<p>
  If we just plot the curve for the first 100 days, we find that the smaller the λ, the more apparent the trend. The curve does not change too much as the λ  smaller than 100. Thus here we choose  λ=100 to extract the trend of daily price data. This trend is our low-frequency component.
</p>

<img class="img-responsive" src="https://cdn.quantconnect.com/tutorials/i/Tutorial08-low-frequency-component-less.jpg" alt="low-frequency-component-less"/>

<p style="text-align: center;">Out-of-sample EUR/USD Trend Estimation</p>

<img class="img-responsive" src="https://cdn.quantconnect.com/tutorials/i/Tutorial08-out-of-sample-trend-estimation.jpg" alt="out-of-sample-trend-estimation" />
<h3>Step 2: Setting up the Moving Average Rule</h3>

<p>
  Moving average (MA) rules are very commonly used to generate buy and sell signals from data on the spot exchange rate. The MA rule compares a short-run moving average of the current and lagged exchange rate with a long-run moving average.
</p>

\[MA(m,n)=\frac{1}{m}\sum_{i=0}^{m-1}S_{t-i}-\frac{1}{n}\sum_{i=0}^{n-1}S_{t-i}\]


<p>
  For HP filter, the non-linear trend is estimated recursively as the paper did. The initial estimation was undertaken using 3 years history data before 2011. The estimation period is then rolled forward each day through the trading period from January 2011 to May 2017.
</p>

<h3>Step 3: Generating the Trading Signals</h3>
<p>
  We generate buy and sell signals by applying an MA(1, 2) rule to the estimated low-frequency component. For MA(m,n), m must be 1 which denotes the current value of low-frequency component. n should be small since large n would generate large time lag, the judgment of turning points is not accurate.
</p>

<p>
  A buy signal is generated when the current day’s low-frequency trend is higher than the last day’s low-frequency trend and a sell signal is generated when it is lower.
</p>

<div class="section-example-container">

<pre class="python">
def OnData(self,data):
    self.close.append(self.Portfolio[self.syl].Price)
    self.hpfilter(self.close[-self.numdays:len(self.close)+1], 100)
    self.MA_rules_today = (np.mean(self.trend[-self.m : len(self.trend)]) - np.mean(self.trend[-self.n : len(self.trend)]))
    self.MA_rules_yesterday = (np.mean(self.trend[-self.m-1: len(self.trend)-1]) - np.mean(self.trend[-self.n-1 : len(self.trend)-1]))
    holdings = self.Portfolio[self.syl].Quantity

    if self.MA_rules_today &gt; 0 and self.MA_rules_yesterday &lt; 0:
       self.SetHoldings(self.syl, 1)
    elif self.MA_rules_today &lt; 0 and self.MA_rules_yesterday &gt; 0:
       self.SetHoldings(self.syl, -1)
</pre>
</div>
<p style="text-align: center;">Trading Signals when λ=1600</p>

<img class="img-responsive" src="https://cdn.quantconnect.com/tutorials/i/Tutorial08-trading-signal-1600.jpg" alt="trading-signal 1600.jpg" />

<p style="text-align: center;">Trading Signals when λ=100</p>
<img class="img-responsive" src="https://cdn.quantconnect.com/tutorials/i/Tutorial08-trading-signal-100.jpg" alt="trading-signal-100.jpg" />

<p>
  The above charts are the in-sample trading signals after applying MA rules on the low-frequency component. The trend curve is more smooth with larger λ. Thus when we applied MA rules, a less smooth trend will trigger more trading opportunities.
</p>
